package com.phx.serialjni;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import java.util.List;

/**

 * @version
 *
 */
public abstract class BaseActivity extends Activity {
	//protected IBackService iBackService;
	protected ServiceConnection connection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			//iBackService = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
	//		iBackService = IBackService.Stub.asInterface(service);
		}
	};
	protected Intent mServiceIntent;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
//            for (Activity activity : ParameterSetActivity.activityList) {
//                activity.finish();
//            }
			this.finish();
			System.exit(0);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	//public abstract boolean onInterceptTouchEvent(MotionEvent ev);

	public void showSystemUI() {
		getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void rotateViewWithAngle(float f, float f2, View view, int i) {
		Animation rotateAnimation = new RotateAnimation(f, f2, 1, 0.5f, 1, 0.5f);
		rotateAnimation.setInterpolator(new LinearInterpolator());
		rotateAnimation.setDuration((long) i);
		//rotateAnimation.setFillAfter(Float.min(0));
		view.startAnimation(rotateAnimation);
	}


//	@Override
//	protected void onStop() {
//		// TODO Auto-generated method stub
//		super.onStop();
//
//		if (!isAppOnForeground()) {
//			//app 进入后台
//
//			//全局变量isActive = false 记录当前已经进入后台
//		}
//	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();


		//if (!isActive) {
		//app 从后台唤醒，进入前台

		//isActive = true;
		//}
	}

	/**
	 * 程序是否在前台运行
	 *
	 * @return
	 */
//	public boolean isAppOnForeground() {
//		// Returns a list of application processes that are running on the
//		// device
//
//		ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
//		String packageName = getApplicationContext().getPackageName();
//
//		List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
//				.getRunningAppProcesses();
//		if (appProcesses == null)
//			return false;
//
//		for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
//			// The name of the process that this object is associated with.
//			if (appProcess.processName.equals(packageName)
//					&& appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
//				return true;
//			}
//		}
//
//		return false;
//	}

}
